package com.testapp.bluetoothtest08.File;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BaseFileOperation {
    //写入
    public void write(File file, String string) {
        try {
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write(string);
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //追加写入
    public void writeAppend(File file, String string) {
        try {
            FileWriter fileWriter = new FileWriter(file,true);
            fileWriter.write(string);
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //读取文件中的字符串
    public String readString(File file) {
        try {
            String result;
            FileReader fileReader = new FileReader(file);
            BufferedReader br = new BufferedReader(fileReader);
            result = br.readLine();
            fileReader.close();
            br.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //读取int
    public int readInt(File file) {
        try {
            String result;
            FileReader fileReader = new FileReader(file);
            BufferedReader br = new BufferedReader(fileReader);
            result = br.readLine();
            fileReader.close();
            br.close();
            return Integer.parseInt(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //读取long
    public long readLong(File file) {
        try {
            String result;
            FileReader fileReader = new FileReader(file);
            BufferedReader br = new BufferedReader(fileReader);
            result = br.readLine();
            fileReader.close();
            br.close();
            return Long.parseLong(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    //判断是否存在
    public boolean isExist(File file) {
        if (file.getParentFile().exists()) {
            return true;
        }
        return false;
    }
}
